from flask_sqlalchemy import SQLAlchemy

db = SQLAlchemy()


class User(db.Model):
    __tableName__ = 'user'  # 数据库中的表名
    id = db.Column(db.Integer, primary_key=True, name='user_id')
    username = db.Column(db.String(30), unique=True, name='user_name')
    password = db.Column(db.String(50), name='user_password')
    power = db.Column(db.Integer, name='user_power')
    picture = db.Column(db.String(255), name='user_picture')

    # 构造函数
    def __init__(self, username, password, power, picture):
        self.username = username
        self.password = password
        self.power = power
        self.picture = picture

    # 控制台直接输出展示函数
    def __repr__(self):
        return f'<User {self.username}>'


class Book(db.Model):
    __tableName__ = 'book'  # 数据库中的表名
    id = db.Column(db.Integer, primary_key=True, name='book_id')
    name = db.Column(db.String(30), unique=True, name='book_name')
    author = db.Column(db.String(30), name='book_author')
    introduction = db.Column(db.String(500), name='book_introduction')
    picture = db.Column(db.String(255), name='book_picture')
    num = db.Column(db.Integer, name='book_num')
    price = db.Column(db.Double, name='book_price')
    score = db.Column(db.Double, name='book_score')
    sort = db.Column(db.String(10), name='book_sort')
    code = db.Column(db.String(255), name='book_code')
    location = db.Column(db.String(50), name='book_location')

    # 构造函数
    def __init__(self, name, author, introduction, picture, num, price, score, sort, code, location):
        self.name = name
        self.author = author
        self.introduction = introduction
        self.picture = picture
        self.num = num
        self.price = price
        self.score = score
        self.sort = sort
        self.code = code
        self.location = location

    # 控制台直接输出展示函数
    def __repr__(self):
        return f'<Book {self.name}>'


class Borrow(db.Model):
    __tableName__ = 'borrow'  # 数据库中的表名
    id = db.Column(db.Integer, primary_key=True, name='borrow_id')
    user_id = db.Column(db.Integer, name='user_id')
    book_id = db.Column(db.Integer, name='book_id')
    borrow_time = db.Column(db.DateTime, name='borrow_time1', server_default=db.func.now())
    return_time = db.Column(db.DateTime, name='borrow_time2', server_default=db.func.now())
    num = db.Column(db.Integer, name='borrow_num')

    # 构造函数
    def __init__(self, user_id, book_id, num):
        self.user_id = user_id
        self.book_id = book_id
        self.num = num

    # 控制台直接输出展示函数
    def __repr__(self):
        return f'<Borrow {self.user_id}>'


class Buy(db.Model):
    __tableName__ = 'buy'
    buy_id = db.Column(db.Integer, primary_key=True, name='buy_id')
    user_from_id = db.Column(db.Integer, name='user_from_id')
    user_to_id = db.Column(db.Integer, name='user_to_id')
    library_name = db.Column(db.String(30), name='library_name')
    borrow_time = db.Column(db.DateTime, name='buy_time1', server_default=db.func.now())
    return_time = db.Column(db.DateTime, name='buy_time2')
    num = db.Column(db.Integer, name='buy_num')
    location = db.Column(db.String(50), name='buy_location')

    # 构造函数
    def __init__(self, user_from_id, user_to_id, library_name, num, location):
        self.user_from_id = user_from_id
        self.user_to_id = user_to_id
        self.library_name = library_name
        self.num = num
        self.location = location

    # 控制台直接输出展示函数
    def __repr__(self):
        return f'<Buy {self.user_id}>'


class Comment(db.Model):
    __tableName__ = 'comment'
    id = db.Column(db.Integer, primary_key=True, name='comment_id')
    book_id = db.Column(db.Integer, name='book_id')
    name = db.Column(db.String(30), name='comment_name')
    time = db.Column(db.DateTime, name='comment_time', server_default=db.func.now())
    content = db.Column(db.String(255), name='comment_content')
    score = db.Column(db.Double, name='comment_score')
    picture = db.Column(db.String(255), name='comment_picture')

    # 构造函数
    def __init__(self, book_id, name, content, score, picture):
        self.book_id = book_id
        self.name = name
        self.content = content
        self.score = score
        self.picture = picture

    # 控制台直接输出展示函数
    def __repr__(self):
        return f'<Comment {self.book_id}>'


class Information(db.Model):
    __tableName__ = 'information'
    id = db.Column(db.Integer, primary_key=True, name='information_id')
    user_id = db.Column(db.Integer, name='user_id')
    sort = db.Column(db.String(10), name='information_sort')
    time = db.Column(db.DateTime, name='information_time', server_default=db.func.now())
    content = db.Column(db.String(255), name='information_content')

    # 构造函数
    def __init__(self, user_id, sort, content):
        self.user_id = user_id
        self.sort = sort
        self.content = content

    # 控制台直接输出展示函数
    def __repr__(self):
        return f'<Information {self.user_id}>'


class Library(db.Model):
    __tableName__ = 'library'
    library_name = db.Column(db.String(10), primary_key=True, name='library_name')
    author = db.Column(db.String(10), name='library_author')
    information = db.Column(db.String(255), name='library_information')
    picture = db.Column(db.String(255), name='library_picture')
    num = db.Column(db.Integer, name='library_num')
    price = db.Column(db.Double, name='library_price')
    user_id = db.Column(db.Integer, db.ForeignKey('user.user_id'), name='user_id')
    user_name = db.Column(db.String(255), name='user_name')

    # 构造函数
    def __init__(self, library_name, author, information, picture, num, price, user_id, user_name):
        self.library_name = library_name
        self.author = author
        self.information = information
        self.picture = picture
        self.num = num
        self.price = price
        self.user_id = user_id
        self.user_name = user_name

    # 控制台直接输出展示函数
    def __repr__(self):
        return f'<Library {self.library_name}>'


class ReturnVO:
    # 构造函数
    def __init__(self, borrow_r, book):
        self.id = borrow_r.id
        self.user_id = borrow_r.user_id
        self.book_id = borrow_r.book_id
        self.return_time = borrow_r.return_time
        self.borrow_time = borrow_r.borrow_time
        self.num = borrow_r.num
        self.book_name = book.name
        self.remain_num = book.num

    # 控制台直接输出展示函数
    def __repr__(self):
        return f'<Borrow {self.book_name}>'


class BorrowVO:
    # 构造函数
    def __init__(self, borrow, book):
        self.id = borrow.id
        self.user_id = borrow.user_id
        self.book_id = borrow.book_id
        self.borrow_time = borrow.borrow_time
        self.return_time = borrow.return_time
        self.num = borrow.num
        self.book_name = book.name
        self.remain_num = book.num

    # 控制台直接输出展示函数
    def __repr__(self):
        return f'<Borrow {self.book_name}>'


class BuyVO:
    # 构造函数
    def __init__(self, buy, library_from):
        self.buy_id = buy.buy_id
        self.user_from_id = buy.user_from_id
        self.user_to_id = buy.user_to_id
        self.library_name = buy.library_name
        self.borrow_time = buy.borrow_time
        self.return_time = buy.return_time
        self.num = buy.num
        self.location = buy.location
        self.library_from_num = library_from.num
        self.library_from_price = library_from.price

    # 控制台直接输出展示函数
    def __repr__(self):
        return f'<Borrow {self.buy_id}>'
